﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Assembler_Compiler
{
    static class ConverterClass
    {
        // converts string to dec or hex int
        public static Int16 StringToInt(string str)
        {
            if (str == "?")
            {
                return 0;
            }

            Int16 result = 0;
            Int16 digit = 1;

            if (str.EndsWith("D") || str.EndsWith("d"))
            {
                // operand is decimal
                Int16 temp;
                for (int i = str.Length - 2; i >= 0; i--)
                {
                    temp = (Int16)(Convert.ToInt16(str[i]) - 48);
                    result += (Int16)(digit * temp);
                    digit *= 10;
                }
            }

            if (str.EndsWith("H") || str.EndsWith("h") || IsHexadecimal(str))
            {
                // operand is hexadecimal
                Int16 temp;
                for (int i = str.Length - 1; i >= 0; i--)
                {
                    if (str[i] == 'H' | str[i] == 'h')
                    {
                        continue;
                    }

                    int t;
                    if(!int.TryParse(str[i].ToString(), out t))
                    {
                        switch (str[i])
                        {
                            case 'A':
                            case 'a':
                                temp = 10;
                                break;
                            case 'B':
                            case 'b':
                                temp = 11;
                                break;
                            case 'C':
                            case 'c':
                                temp = 12;
                                break;
                            case 'D':
                            case 'd':
                                temp = 13;
                                break;
                            case 'E':
                            case 'e':
                                temp = 14;
                                break;
                            case 'F':
                            case 'f':
                                temp = 15;
                                break;
                            default:
                                throw new Exception("Can not convert " + str + " to hexadecimal integer.");
                        }
                    }
                    else
                    {
                        temp = (Int16)t;
                    }

                    result += (Int16)(digit * temp);
                    digit *= 16;
                }
            }

            return result;
        }

        public static Int16 FromBinaryStringToInt(string str)
        {
            Int16 result = 0;
            Int16 digit = 1;
            Int16 temp;
            for (int i = str.Length - 1; i >= 0; i--)
            {
                if (str[i] == '0')
                {
                    temp = 0;
                }
                else
                {
                    if (str[i] == '1')
                    {
                        temp = 1;
                    }
                    else
                    {
                        throw new Exception("Invalid binary string.");
                    }
                }

                result += (Int16)(digit * temp);
                digit *= 2;
            }

            return result;
        }

        public static bool IsDecimal(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (!char.IsDigit(str, i))
                {
                    return false;
                }
            }

            return true;
        }

        public static bool IsHexadecimal(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                char test = str[i];
                if (!char.IsDigit(str, i) && str[i] != 'A' && str[i] != 'B' && str[i] != 'C' && str[i] != 'D' && str[i] != 'F')
                {
                    return false;
                }
            }

            return true;
        }
    }
}
